// cookie 操作公共方法
export default {
    setCookie: function (name, value, exdays) {
      let d = new Date()  //获取时间
      d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)) //保存天数
      var expires = 'expires=' + d.toUTCString()
      document.cookie = name + '=' + value + ';path=/;' + expires
    },
    getCookie: function (name) {
      let arr
      let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      arr = document.cookie.match(reg)
      if (arr) {
        return (arr[2])
      } else {
        return ''
      }
    },
    delCookie: function (name) {
      let exp = new Date()
      exp.setTime(exp.getTime() - 1)
      var cval = this.getCookie(name)
      if (cval != null || cval !== '') {
        document.cookie = name + '=' + cval + ';path=/;expires=' + exp.toGMTString()
      }
    },
    clearCookie: function () {
      var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
      if(keys) {
        for(var i = keys.length; i--;) {
          document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString()
        }
      }
    },
    checkCookie: function () {
      let token = this.getCookie('token')
      let userid = this.getCookie('userid')
      if (token && userid) {
        return true
      }
      return false
    }
  }
  